package com.javaee.xiangyang.controller.Admin;

import com.javaee.xiangyang.common.ServiceResultEnum;
import com.javaee.xiangyang.dto.OrderGoods;
import com.javaee.xiangyang.entity.Order;
import com.javaee.xiangyang.entity.enmu.impl.OrderState;
import com.javaee.xiangyang.service.OrderService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import java.util.List;

@Controller
@RequestMapping("/Orders")
public class OrdersController {

    @Resource
    OrderService orderService;

    //转入订单管理界面
    @RequestMapping("/Management")
    public ModelAndView getOrdersManagementList(ModelAndView modelAndView) {
        //获取所有订单信息
        List<OrderGoods> allOrder = orderService.findAllOrder();
        modelAndView.addObject("allOrder", allOrder);
        modelAndView.setViewName("admin/order_list");
        return modelAndView;
    }

    //根据订单状态查找
    @RequestMapping("/State")
    public ModelAndView getOrderManagementListByState(ModelAndView modelAndView, Order order) {
        for (OrderState state : OrderState.values()) {
            if (state == order.getOrderState()) {
                //存在此状态
                //根据订单状态查找对应状态的订单
                List<OrderGoods> orderByState = orderService.findOrderByState(order);
                modelAndView.addObject("allOrder", orderByState);
                //传入订单状态
                modelAndView.addObject("state", order.getOrderState().getCode());
                modelAndView.setViewName("admin/order_list");
                return modelAndView;
            }
        }
        modelAndView.setViewName("redirect:/Orders/Management");
        return modelAndView;
    }

    //修改订单状态
    @RequestMapping("/Update")
    public ModelAndView updateOrderState(ModelAndView modelAndView, Order order) {
        orderService.updateOrderStateByOrderId(order);
        modelAndView.setViewName("redirect:/Orders/Management");
        return modelAndView;
    }

}
